var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		jxCourseType: {}
	},
	methods: {
        getjxCourse: function(){
            //加载课程树
            $.get(baseURL + "sys/jxcoursetype/select", function(r){
                ztree = $.fn.zTree.init($("#jxCourseTree"), setting, r.jxCourseList);
                var node = ztree.getNodeByParam("id", vm.jxCourseType.parentId);
                ztree.selectNode(node);
                vm.jxCourseType.parentName = node.name;
            })
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.jxCourseType = {parentName:"上级课程",delFlag:0};
			vm.getjxCourse();
		},
		update: function (event) {
            var id  = getjxCourseId();
            if(id  == null){
                return ;
            }

            $.get(baseURL + "sys/jxcoursetype/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.jxCourseType = r.jxCourseType;

                vm.getjxCourse();
            });

		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.jxCourseType.id == null ? "sys/jxcoursetype/save" : "sys/jxcoursetype/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.jxCourseType),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
            var id = getjxCourseId();
            if(id == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/jxcoursetype/delete",
                    data: "id=" + id,
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/jxcoursetype/info/"+id, function(r){
                vm.jxCourseType = r.jxCourseType;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        jxCourseTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择课程",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#jxCourseLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.jxCourseType.parentId = node[0].id;
                    vm.jxCourseType.parentName = node[0].name;
                    vm.jxCourseType.level = node[0].level;
                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            jxcoursetype.table.refresh();
        }
	}
});

var jxcoursetype = {
    id: "jxCourseTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
jxcoursetype.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '课程ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '课程名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '上级', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '排序号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        /*{title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true, width: '100px',formatter:function(row, value, index){
            console.log( index);
                console.log( row);
                var text="<a class='btn btn-default ' onclick='showfile("+value+")' ><i class='fa fa-file-word-o' style='font-size: 20px'></i>&nbsp;资料管理</a>";
                text=text+"&nbsp;&nbsp;&nbsp;<a class='btn btn-info' onclick='showCoruseSchedule("+value+")' ><i class='fa fa-calendar-plus-o' style='font-size: 20px'></i>&nbsp;排课</a>"
                return text;
            }}*/
        ]
    return columns;
};


function getjxCourseId () {
    var selected = $('#jxCourseTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
    $.get(baseURL + "sys/jxcoursetype/info", function(r){
        var colunms = jxcoursetype.initColumn();
        var table = new TreeTable(jxcoursetype.id, baseURL + "sys/jxcoursetype/list", colunms);
        table.setRootCodeValue(r.id);
        table.setExpandColumn(2);
        table.setIdField("id");
        table.setCodeField("id");
        table.setParentCodeField("parentId");
        table.setExpandAll(true);
        table.init();
        jxcoursetype.table = table;
    });
});

